<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Shop.Classes.DAO
 * @package   PsiFramework.Plugins.Shop.Classes.DAO
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\ItemCategoryDAO.html
 */

namespace PsiFramework\Plugins\Shop\Classes\DAO;

use PsiFramework\Classes\Util\Database\Query\SqlFunction;

import("Classes.DAO.AbstractDAO");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;


/**
 * The ItemCategory Data Access Object
 *
 * This is the default class for the ItemCategory Data Access Object
 *
 * @category PsiFramework.Plugins.Shop.Classes.DAO
 * @package  PsiFramework.Plugins.Shop.Classes.DAO
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Shop\Classes\DAO\ItemCategoryDAO.html
 */
class ItemCategoryDAO extends AbstractDAO
{
    /**
     * The singleton
     * 
     * @var ItemCategoryDAO
     */
    protected static     $_singleton = null;

    /**
     * ItemCategoryDAO constructor
     * 
     * @param ObjectParameters $objectParameters The object parameters
     * 
     * @return ItemCategoryDAO
     */
    protected function __construct(
        ObjectParameters $objectParameters=null
    )
    {
        parent::__construct($objectParameters);
    }
    
    /**
     * Retrieve a category by name
     *
     * @param string $name          The name to search
     * @param bool   $checkSecurity Check security ?
     *
     * @return OrderItem
     */
    public function retrieveByName(
        /*string*/ $name,
    	/*bool*/   $checkSecurity=true
    )
    {
    	$items = $this->retrieveByPartialName($name, true, $checkSecurity);
    	
    	if (!is_null($items) && count($items)>0) {
    		return $items[0] ;
    	} else {
    		return null ;
    	}
    }
    
    /**
     * Retrieve a category by partial name
     *
     * @param string $name          The name to search
     * @param bool   $exactName     Search by exact name ?
     * @param bool   $checkSecurity Check security ?
     *
     * @return OrderItem
     */
    public function retrieveByPartialName(
        /*string*/ $name,
        /*bool*/   $exactName=false,
        /*bool*/   $checkSecurity=true
    )
    {
    	$col = null;
    	$value = null;
    	$equal = null;
    	if ($exactName) {
    	    $value = new SqlValue($name);
    	    $col = new SqlColumn('category_name');
    	    $equal = new SqlOperation(SqlOperation::$equal);
    	} else {
    	    $value = new SqlValue('%'.mb_strtoupper($name, 'utf-8').'%');
    	    $col = new SqlFunction("UPPER");
    	    $col->addParameter(new SqlColumn('category_name'));
    	    $equal = new SqlOperation(SqlOperation::$like);
    	}
    	
    	$equal->addParameter($col);
    	$equal->addParameter($value);
    
    	return $this->retrieveAll(new QueryFilter($equal), null, $checkSecurity);
    }
    
    /**
     * The function to call instead of the constructor.
     *
     *@param ObjectParameters $objectParameters the object parameters 
     *                                          for Order
     *
     * @return ItemCategoryDAO instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new ItemCategoryDAO($objectParameters);
        }

        return self::$_singleton;
    }
}
